<?php
namespace core;
        
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

/**
 * http
 */
class Log 
{
    //日志配置
    public $config =[
        //是否开启
        'enable' => false,
        //日志目录
        'path' => GKRUNTIME,
    ];
    
    //日志配置
    public static  $log;
    
    /**
     * 构造函数
     * @param $config 配置文件
     * 
     */
    public  function __construct($config)
    {
       if($config){
           $this->config = $config;
       }else{
           
       }
       self::$log = new Logger('task');
       $this->setFileName();
    }
    
     /**
     * 设置log文件名称
     * @params $file_name String 文件名称
     */
    public  function setFileName($file_name = '')
    {   
        $dir = $this->config['path'];
        $dir = $dir .'/';
        if(!$file_name){
            $file_name = $dir.date("Y-m-d").'.log';
        }else{
            $file_name = $dir.$file_name.'.log';
        }
        if (is_file($file_name) && 1024*1024*$this->config['file_size'] <= filesize($file_name)) {
            try {
                rename($file_name, $dir.date("Y-m-d H:i:s",time()).'.log');
            } catch (\Exception $e) {
                
            }
        }
        self::$log->pushHandler(new StreamHandler($file_name));
    }
    
     /**
     * 文件压缩
     */
    public  function logzip( )
    {   
        $dir = new \RecursiveDirectoryIterator($this->config['path']);
        $totalSize = 0;
        foreach (new \RecursiveIteratorIterator($dir) as $file) {
          $totalSize += $file->getSize();
        }
        $sizeMb = $totalSize/1024/1024;
        
        if($sizeMb > $this->config['zip_runtime_size']){
            //压缩目录
            $makeZip = new MakeZip();
            $zipName = GZROOT_PATH.'runtimezip'.DS.date("Y-m-d H:i:s",time()).'.zip';
            if (! is_file($zipName)) {
                $fp = fopen($zipName, 'w');
                fclose($fp);
            }
            $zip = new \ZipArchive();
            if ($zip->open($zipName, \ZipArchive::OVERWRITE) === true) {
                $current = $this->config['path']; // 你要压缩的文件的主目录
                $path = $this->config['path'];
                $makeZip->addFileToZip($path, $current, $zip);
                $zip->close();
                foreach (new \RecursiveIteratorIterator($dir) as $file) {
                  if(is_file($file) && $file->getSize() >= 1024*1024*$this->config['file_size']){
                     unlink($file);
                  }
                }
            }else{
               $this->debug('压缩失败');
            }
        }
    }
    
     /**
     * 直接设置log
     * @param string $message The log message
     * @param array  $context The log context
     */
    public function log($level, $message, array $context = [])
    {   
        self::$log->log($level, $message, $context);
    }
    
     /**
     * 设置debug级别
     * @param string $message The log message
     * @param array  $context The log context
     * @param bool   $flag  是否记录
     */
    public function debug($message, array $context = [],$flag =false)
    {   
        if($flag){
            self::$log->debug($message, $context);
        }
    }
    
     /**
     * 设置info级别
     * @param string $message The log message
     * @param array  $context The log context
     */
    public function info($message, array $context = [])
    {   
        self::$log->info($message, $context);
    }
    
     /**
     * 设置warning级别
     * @param string $message The log message
     * @param array  $context The log context
     */
    public function warning($message, array $context = [])
    {   
        self::$log->warning($message, $context);
    }
    
     /**
     * 设置error级别
     * @param string $message The log message
     * @param array  $context The log context
     */
    public function error($message, array $context = [])
    {   
        $filename = date("Y-m-d").'-error';
        $this->setFileName($filename);
        self::$log->error($message, $context);
    }
    
    
}
